# -*-coding:utf-8 -*- #
# ---------------------------------------------------------------------------
# ProjectName:   selenium_self
# FileName:      login_page.py
# Author:       test_liu
# Datetime:     2024/10/17 下午8:12
# Description: 继承basic类，实现登录页面操作
# ---------------------------------------------------------------------------
import time

from selenium.webdriver import Keys

from WebTest3.WebTest.basic.basic import Basic
from WebTest3.WebTest.common.read_local_ini import ReadLocalIni
from WebTest3.WebTest.common.read_yaml import read_yaml


class LoginPage(Basic):
    # 获取yaml文件中的所有定位
    datas = read_yaml(ReadLocalIni().get_yaml('yaml'))

    def __init__(self, web='e'):
        super().__init__(web)

    def login(self, username, password):
        # 登录操作，用户名、密码、验证码、登录按钮
        self.send_key(self.datas['登录页面']['用户名'], username)
        self.send_key(self.datas['登录页面']['password'], password)
        # 获取验证码，验证码能抓包获得
        codes = self.presences(self.datas['登录页面']['codes'])
        # 获取验证码
        code = ""
        for i in codes:
            code += i.text
        # 填写验证码
        self.send_key(self.datas['登录页面']['code'], Keys.LEFT_CONTROL + 'a')
        self.send_key(self.datas['登录页面']['code'], Keys.LEFT_CONTROL + 'x')
        self.send_key(self.datas['登录页面']['code'], code)
        self.click(self.datas['登录页面']['login'])
        time.sleep(0.5)
        res = self.presences(self.datas['登录页面']['login_res'])[-1].text
        return res

    def close(self):
        self.driver.quit()


if __name__ == '__main__':
    login = LoginPage()
    login.login("123", "123")
